package br.com.iswe.gwt.ui.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import br.com.iswe.gwt.ui.client.img.ISWEImages;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;

public class DateBox extends Composite implements FormProperty {

	private static final String IMG = "_img";
	private static final String ID = "id";
	private static final String GWT_TEXT_BOX_DATE = "gwt-TextBox-date";
	
	protected HorizontalPanel horizontalPanel;
	protected TextBox dateTextBox;
	protected Image   imgCalendar;
	
	protected ISWEImages isweImages = (ISWEImages)GWT.create(ISWEImages.class);
	
	private boolean enabled = true;
	
	public DateBox(){
		//Inicia os componentes
		this.imgCalendar = isweImages.calendario().createImage();
		this.dateTextBox = new TextBox();
		this.horizontalPanel = new HorizontalPanel();
		this.horizontalPanel.add(dateTextBox);
		this.horizontalPanel.add(imgCalendar);
		initWidget(this.horizontalPanel);
		
		//estilo do date
		this.dateTextBox.addStyleName(GWT_TEXT_BOX_DATE);
		//Configura os validadores para data
		this.dateTextBox.addKeyUpHandler(DateValidator.DATE_CONSTRAINTS);
		//configura o maxlength
		this.dateTextBox.setMaxLength(10);
	}

	public TextBox getDateTextBox() {
		return dateTextBox;
	}

	public void setDateTextBox(TextBox dateTextBox) {
		this.dateTextBox = dateTextBox;
	}
	
	protected void abrirCalendario(){
		//Ao clicar na imagem abre o calendario
		CalendarBox.getInstance().showFor(this);
	}

	protected void onLoad() {
		//apos ter sido adicionado ao document configura o calendar
		//ClientUtils.setupCalendar(this.dateTextBox, this.imgCalendar);
		
		imgCalendar.addClickHandler(new ClickHandler(){public void onClick(ClickEvent event) {
			abrirCalendario();
		}});
		
	}
	
	protected String name = null;
	public void setName(String nome) {
		this.name = nome;
		this.dateTextBox.setName(nome);
		//configura o id dos objetos.
		DOM.setElementProperty(this.dateTextBox.getElement(), ID, nome.replace('.', '_'));
		DOM.setElementProperty(this.imgCalendar.getElement(), ID, nome.replace('.', '_') + IMG);
	}
	public String getName() {
		return name;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
		if(this.dateTextBox != null){
			this.dateTextBox.setEnabled(enabled);
			this.imgCalendar.setVisible(enabled);
		}
	}
	
	public void clear() {
		getDateTextBox().setText("");		
	}

	public void fireOnChangeEvent(Object source){
		//varre a lista de change listener
		for (Listener<Object> handler : changeListeners) {
			handler.execute(source);
		} 
	}
	
	private List<Listener<Object>> changeListeners = new ArrayList<Listener<Object>>();
	
	public void addChangeListener(Listener<Object> listener) {
		changeListeners.add(listener);
		
		getDateTextBox().addChangeHandler(new ChangeHandler(){public void onChange(ChangeEvent event) {
			fireOnChangeEvent(event.getSource());
		}});
		getDateTextBox().addBlurHandler(new BlurHandler(){public void onBlur(BlurEvent event) {
			fireOnChangeEvent(event.getSource());
		}});
	}

	public String getValue() {
		return getDateTextBox().getText();
	}

	public boolean isEnabled() {
		return enabled;
	}
	
	public void setValue(Object value) {
		if(value == null){
			getDateTextBox().setText("");
		}
		else if(value instanceof String){
			getDateTextBox().setText((String)value);
		}
		else if(value instanceof Date){
			throw new RuntimeException("Not implemented yet !!! DateBox --> setValue(java.util.Date(");
		}
		else{
			throw new RuntimeException("DateBox.setValue() type not supported: " + value.getClass().getName());
		}
	}

	
}
